package com.sixi.GeoTransfer;


import static com.sixi.GeoTransfer.Constants.*;

import java.awt.geom.Point2D;
import java.awt.geom.Point2D.Double;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.sixi.GeoTransfer.model.AreaModel;
import com.sixi.GeoTransfer.model.City;
import com.sixi.GeoTransfer.model.Province;

public class TransferGeoInfo implements ITransferGeoInfo {

	public static List<AreaModel> areaInfoList;
	public static List<Province> provinceInfoList;
	public static List<City> cityInfoList;
	
	
	@Override
	public String getGeoInfoFromCoordinate(Point2D.Double coordinate, String locationLevel) {
		//1. 根据locationLevel加载对应的area_info.tb文件到静态内存中。
		if(areaInfoList== null) {
			System.out.println("调用loadAreaInfo2Mem方法");
			loadAreaInfo2Mem(locationLevel);
		}
		
		AreaModel returnArea = null;
		//2. 根据输入的点和获取到静态内存中的List,调用RatInCatUtils.ratInCat方法来判定点所在的区域。
		if(areaInfoList!=null) {
			returnArea = RatInCatUtils.ratInCatAreaModel(coordinate, areaInfoList);
		}
		
		
		//3. 根据返回的区域，组装json串返回给调用方。
		String returnJsonStr = assembleAreaInfoJson(returnArea, locationLevel);
		
		return returnJsonStr;
	}

	
	private void loadAreaInfo2Mem(String locationLevel) {
		switch (locationLevel){
			case  LOCATION_LEVEL_PROVINCE: 
				areaInfoList = LoadAreaInfo.loadProvincesInfo();
				break;
			case LOCATION_LEVEL_CITY:
				areaInfoList = LoadAreaInfo.loadCityInfo();
				break;
			case LOCATION_LEVEL_AREA:
				System.out.println("not support area transfer yet");
				break;
			default:
				System.out.println("Cannot recognise locationLevel, hence loading city info by default");
				areaInfoList = LoadAreaInfo.loadCityInfo();
				break;
		}
	}
	
	/**
	 * 0000：正常返回
	 * 0001：找不到
	 * 
	 * 
	 * @param returnArea
	 * @param locationLevel
	 * @return
	 */
	private String assembleAreaInfoJson(AreaModel returnArea, String locationLevel) {
		String json = "{ }";
		Map<String, Object> returnMap = new HashMap<String, Object>();
		if(returnArea !=null) {
			switch(locationLevel){
			case  LOCATION_LEVEL_PROVINCE: 
				Province pv = (Province) returnArea;
				returnMap.put("status", "0000");
				returnMap.put("location_level", LOCATION_LEVEL_PROVINCE);
				
				Map<String, String> provinceMap = new HashMap<String, String>();
				provinceMap.put("country", pv.getCountry());
				provinceMap.put("name", pv.getName());
				
				returnMap.put("area_info", provinceMap);
				
				json = JSON.toJSONString(returnMap);
				break;
			case LOCATION_LEVEL_CITY:
				City ct = (City)returnArea;
				returnMap.put("status", "0000");
				returnMap.put("location_level", LOCATION_LEVEL_CITY);
				
				Map<String, String> cityMap = new HashMap<String, String>();
				cityMap.put("country", ct.getCountry());
				cityMap.put("province", ct.getProvince());
				cityMap.put("name", ct.getName());
				cityMap.put("adCode", ct.getAdCode());
				
				returnMap.put("area_info", cityMap);
				json = JSON.toJSONString(returnMap);
				break;
			case LOCATION_LEVEL_AREA:
				System.out.println("not support area transfer yet");
				break;
			default:
				System.out.println("Cannot recognise locationLevel, hence loading city info by default");
				break;
			}
		}else {
			returnMap.put("status", "0001");
			returnMap.put("location_level", locationLevel);
			
			returnMap.put("area_info", "cannot locate");
			json = JSON.toJSONString(returnMap);
		}
		return json;
	}


	@Override
	public String getCityInfoFromCoordinate(Point2D.Double coordinate, String province) {
		if(coordinate == null) {
			return "{ ERROR : 'coordinate' is null }";
		}
		
		if(province !=null && !province.isEmpty() && cityInfoList==null) {
			this.selectProvinceToLoad(province);
		}
		AreaModel returnArea = null;
		if(cityInfoList!=null) {
			returnArea = RatInCatUtils.ratInCatAreaModel(coordinate, cityInfoList);
		}
		
		String returnJsonStr = assembleAreaInfoJson(returnArea, LOCATION_LEVEL_CITY);
		
		return returnJsonStr;
	}


	@Override
	public String getCityInfoFromCoordinate(Double coordinate) {
		return this.getCityInfoFromCoordinate(coordinate, CHINA_PROVINCE_ALL);
	}

	
	private void selectProvinceToLoad(String province){
			switch(province) {
			case CHINA_PROVINCE_BEIJING:
				loadCityInfo2Mem("北京");
				break;
			case CHINA_PROVINCE_TIANJIN:
				loadCityInfo2Mem("天津");
				break;
			case CHINA_PROVINCE_SHANGHAI:
				loadCityInfo2Mem("上海");
				break;
			case CHINA_PROVINCE_CHONGQING:
				loadCityInfo2Mem("重庆");
				break;
			case CHINA_PROVINCE_HEBEI:
				loadCityInfo2Mem("河北");
				break;
			case CHINA_PROVINCE_SHAN1XI:
				loadCityInfo2Mem("山西");
				break;
			case CHINA_PROVINCE_NEIMENGGU:
				loadCityInfo2Mem("内蒙古");
				break;
			case CHINA_PROVINCE_LIAONING:
				loadCityInfo2Mem("辽宁");
				break;
			case CHINA_PROVINCE_HEILONGJIANG:
				loadCityInfo2Mem("黑龙江");
				break;
			case CHINA_PROVINCE_QINGHAI:
				loadCityInfo2Mem("青海");
				break;
			case CHINA_PROVINCE_SICHUAN:
				loadCityInfo2Mem("四川");
				break;
			case CHINA_PROVINCE_GUIZHOU:
				loadCityInfo2Mem("贵州");
				break;
			case CHINA_PROVINCE_YUNNAN:
				loadCityInfo2Mem("云南省");
				break;
			case CHINA_PROVINCE_SHAN3XI:
				loadCityInfo2Mem("陕西");
				break;
			case CHINA_PROVINCE_GANSU:
				loadCityInfo2Mem("甘肃");
				break;
			case CHINA_PROVINCE_NINGXIA:
				loadCityInfo2Mem("宁夏");
				break;
			case CHINA_PROVINCE_XINJIANG:
				loadCityInfo2Mem("新疆");
				break;
			case CHINA_PROVINCE_XIZANG:
				loadCityInfo2Mem("西藏");
				break;
			case CHINA_PROVINCE_HAINAN:
				loadCityInfo2Mem("海南");
				break;
			case CHINA_PROVINCE_JIANGSU:
				loadCityInfo2Mem("江苏");
				break;
			case CHINA_PROVINCE_ZHEJIANG:
				loadCityInfo2Mem("浙江");
				break;
			case CHINA_PROVINCE_ANHUI:
				loadCityInfo2Mem("安徽");
				break;
			case CHINA_PROVINCE_FUJIAN:
				loadCityInfo2Mem("福建");
				break;
			case CHINA_PROVINCE_JIANGXI:
				loadCityInfo2Mem("江西");
				break;
			case CHINA_PROVINCE_SHANDONG:
				loadCityInfo2Mem("山东");
				break;
			case CHINA_PROVINCE_HENAN:
				loadCityInfo2Mem("河南");
				break;
			case CHINA_PROVINCE_HUBEI:
				loadCityInfo2Mem("湖北");
				break;
			case CHINA_PROVINCE_HUNAN:
				loadCityInfo2Mem("湖南");
				break;
			case CHINA_PROVINCE_GUANGDONG:
				loadCityInfo2Mem("广东");
				break;
			case CHINA_PROVINCE_GUANGXI:
				loadCityInfo2Mem("广西");
				break;
			case CHINA_PROVINCE_XIANGGANG:
				loadCityInfo2Mem("香港");
				break;
			case CHINA_PROVINCE_AOMEN:
				loadCityInfo2Mem("澳门");
				break;
			case CHINA_PROVINCE_TAIWAN:
				loadCityInfo2Mem("台湾");
				break;
			case CHINA_PROVINCE_ALL:
				loadCityInfo2Mem();
				break;
			default:
				loadCityInfo2Mem();
				break;
		}
	}
	
	
	private void loadCityInfo2Mem(String provinceName) {
		cityInfoList = LoadAreaInfo.loadCityInfo(provinceName);
		
	}
	
	private void loadCityInfo2Mem(){
		loadCityInfo2Mem(null);
	}
	
}
